package com.example.song.wlms;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.song.wlms.adapter.ActivityAdapter;
import com.example.song.wlms.fragment.DmensionFragment;
import com.example.song.wlms.fragment.HomeFragment;
import com.example.song.wlms.fragment.IdolFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Fragment> fragmentList = new ArrayList<>();
    private RadioGroup radioGroup;
    private ActivityAdapter activityAdapter;
    private RadioButton rb_home;
    private RadioButton rb_idol;
    private RadioButton rb_dmension;
    private ViewPager viewPager;
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initListener();
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_open, R.string.app_close);
        drawerLayout.addDrawerListener(toggle);
    }

    private void initListener() {
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_idol:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_dmension:
                        viewPager.setCurrentItem(2);
                        break;
                }
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    rb_home.setChecked(true);
                } else if (position == 1) {
                    rb_idol.setChecked(true);
                } else if (position == 2) {
                    rb_dmension.setChecked(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
        HomeFragment homeFragment = new HomeFragment();
        IdolFragment idolFragment = new IdolFragment();
        DmensionFragment dmensionFragment = new DmensionFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(idolFragment);
        fragmentList.add(dmensionFragment);
        activityAdapter = new ActivityAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(activityAdapter);
        viewPager.setOffscreenPageLimit(2);
    }

    private void initView() {
        toolbar = ((Toolbar) findViewById(R.id.home_toolBar));
        drawerLayout = ((DrawerLayout) findViewById(R.id.DrawerLayout));
        radioGroup = ((RadioGroup) findViewById(R.id.rg));
        rb_home = ((RadioButton) findViewById(R.id.rb_home));
        rb_idol = ((RadioButton) findViewById(R.id.rb_idol));
        rb_dmension = ((RadioButton) findViewById(R.id.rb_dmension));
        viewPager = ((ViewPager) findViewById(R.id.pager));
    }
}
